package test.workflow;

import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.util.List;

import test.IO.Input;
import test.IO.Output;
import test.Issue.Issue;
import test.config.Config;
import test.http.Connector;
import test.http.HTTPResponse;
import test.http.HTTPClient;

public class Flow {
	
	private static boolean USE_PROXY=Config.isProxy();
	private static String cookie="";

	public static InputStream loginScreen(String location) {
		try {
			HttpURLConnection connection = Connector.openConnection(location, USE_PROXY);
			connection.connect();
			return connection.getInputStream();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static String login(String location, String userName, String password) {
		try {
			String urlParameters = "os_username="
					+ URLEncoder.encode(userName, "UTF-8") + "&os_password="
					+ URLEncoder.encode(password, "UTF-8");

			String loginScreenUrl=location+"login.jsp";
			HttpURLConnection connection = Connector.openConnection(location, USE_PROXY);

			HTTPResponse response = HTTPClient.excutePost(connection, loginScreenUrl,
					urlParameters, cookie);
			
			//Output.inputStreamToSystemOut(response.getReplyInputStream());
			cookie=response.getCookie();
			return Output.inputStreamToString(response.getReplyInputStream());

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static InputStream openIssue(String issueNameBaseLocation,
			String issueName) {
		String issueURL = issueNameBaseLocation + issueName;
		try {
			HttpURLConnection connection = Connector.openConnection(issueURL, USE_PROXY);
			HTTPResponse response = HTTPClient.excutePost(connection, issueURL,
					null, cookie);
			
			//System.out.println(response.getReplyInputStream());
			return response.getReplyInputStream();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static InputStream startWork(String startProgressUrl, Issue issue) {
		try {			
			HttpURLConnection connection = Connector.openConnection(startProgressUrl, USE_PROXY);
			
			String urlParameters = "action="
				+ URLEncoder.encode("41", "UTF-8") + "&id="
				+ URLEncoder.encode(issue.getJiraInernalId(), "UTF-8");
			
			HTTPResponse response = HTTPClient.excutePost(connection, startProgressUrl,
					urlParameters, cookie);
			
			System.out.println(response.getReplyInputStream());
			return response.getReplyInputStream();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static List<Issue> searchForPriorityIssuesToProcess(String issueFilterURL, boolean onlyPriority) {
		HttpURLConnection connection;
		try {
			connection = Connector.openConnection(issueFilterURL, USE_PROXY);
			connection.connect();
			
			HTTPResponse response = HTTPClient.excutePost(connection, issueFilterURL,
					null, cookie);			
			
			return Input.searchForPriorityIssuesToProcess(response.getReplyInputStream(), onlyPriority);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	//for debugging
	public static List<Issue> searchForPriorityIssuesToProcessFromFile(String issuesUrl, boolean onlyPriority) {
		try {
			FileInputStream inputStream = Input.readFile(issuesUrl);
			return Input.searchForPriorityIssuesToProcess(inputStream, onlyPriority);
			
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
}
